package com.ulook.lookplanet.ui.notifications.chat;

import android.util.Log;

import com.ulook.lookplanet.base.BaseViewModel2;
import com.ulook.lookplanet.data.model.TSysMsg;
import com.ulook.lookplanet.ui.notifications.VMsgUser;

import org.litepal.LitePal;

import java.util.List;

/**
 * 描述：
 *
 * @author L1998
 * @data 2021/5/8
 */
public class SyschatViewModel extends BaseViewModel2 {

    private int currUserId;

    public int getCurrUserId() {
        return currUserId;
    }

    public void setCurrUserId(int currUserId) {
        this.currUserId = currUserId;
    }

    /**
     * 获取未读数据
     */
    public List<TSysMsg> getUnreadMsg(){
        List<TSysMsg> unreadList = LitePal.where("userid = ? and state = 0", currUserId+"").order("sendtime").find(TSysMsg.class);
        // 更新数据
        for(int i = 0; i < unreadList.size(); i++){
            updateMsgRead(unreadList.get(i).getUid());
            //unreadList.get(i).setState(1);
            //unreadList.get(i).save();
        }
        return unreadList;
    }

    /**
     * 更新数据为已读
     */
    public void updateMsgRead(String uid){
        TSysMsg tSysMsg = new TSysMsg();
        tSysMsg.setState(1);
        int res = tSysMsg.updateAll("uid = ?", uid);
        //Log.d("#####", "update_sys_msg  res:  "+res);
    }

    /**
     * 获取历史数据
     */
    public List<TSysMsg> getHistoryMsg(int num){
        List<TSysMsg> unreadList = LitePal.where("userid = ? and state = 1", currUserId+"")
                .order("sendtime desc")
                .limit(num)
                .find(TSysMsg.class);
        return unreadList;
    }
}
